#include "udpclient.h"

UdpClient::UdpClient(QObject *parent, quint16 port) : QObject(parent)
{
    this->port = port;
    clientThread = new UdpClientThread(this, port);

    connect(this, &UdpClient::singleMessageSignal,
            clientThread, &UdpClientThread::sendMessageSignal);

}

void UdpClient::start()
{
    if (clientThread != nullptr) {
        clientThread->start();
    }
}

void UdpClient::sendSingleMessage(const QString &message)
{
    QByteArray datagram = message.toUtf8();
    sendSingleMessage(datagram);
}

void UdpClient::sendSingleMessage(const QByteArray &message)
{
    emit singleMessageSignal(message);
}

void UdpClient::setIPandPort(QString ip, quint16 port)
{
    this->ip = ip;
    this->port = port;
}

UdpClient::~UdpClient()
{
    clientThread->exit();
    clientThread->wait();
}
